<?php

/**
 * Created by PhpStorm daichen.
 * User: 橙橙同学
 * Date: 2017/11/22
 * Time: 11:00
 */
namespace app;

class CrmHttpApiTest
{
    const SKEY = 'NmE0MmM2ZjZmODdmMGImYzU4Yjg1UYU2MWE=';
    const CRMHOST = 'http://113.140.27.50:8740/CRMInterface.ashx';

    protected static function getSign()
    {
        $ts = time();
        $signStr = $ts.self::SKEY;
        return array(
            'ts'=>$ts,
            'sig'=>md5($signStr)
        );
    }

    public static function __callStatic($name,$arguments)
    {
        $postData = self::generatePostData($name,$arguments[0]);
        $res = http_post(self::CRMHOST,$postData);
        //如果返回false  http请求失败
        if(!$res)
        {
            $res = json_encode(array('msg'=>'HTTP请求错误'));
        }
        self::MarkResponseData($name,$postData,$res);
    }

    protected static function commonReturn($errCode,$msg)
    {
        echo "<p style='background: #d9edf7;margin-bottom: 1px;padding: 10px;border-radius: 5px'>返回码 {$errCode} 返回信息 {$msg}</p>";
    }

    protected static function generatePostData($method,$args)
    {
        $sin = self::getSign();
        $postData = array(
            "method"=>$method,
            "ts"=>$sin['ts'],
            "sig"=>$sin['sig'],
            "ver"=>1,
            "args"=>$args
        );
        return json_encode($postData);
    }

    protected static function MarkResponseData($method,$postData,$data)
    {
        $insertRes = pdo_insert('api_record',array(
                'api'=>$method,
                'request'=>$postData,
                'response'=>$data,
                'create_time'=>time(),
                'apitype'=>'crm',
                'request_type'=>'post'
            )
        );
        if($insertRes)
        {
            self::commonReturn(0,'接口'.$method.' 测试结果记录成功');
        }
        else{
            self::commonReturn(-1,'接口'.$method.' 测试结果记录失败');
        }
    }


    /**
     * @author:橙橙同学
     * @func:绑卡
     */
    public static function dataBindCard()
    {
        $args = array(
            'condType'=>1,
            'condValue'=>'user_id',
            'name'=>'戴晨',
            'idno'=>'142703198701143319',
            'phone'=>15398091110
        );
        return $args;
    }


    public static function dataOpenCard()
    {

        $args = array(
            'condType'=>1,
            'condValue'=>'hahahahahah',
            'idno'=>'610429199503101718',
            'phone'=>18409271457,
            'name'=>'戴晨',
            'gender'=>1,
            'birthday'=>'1995-03-10',
            'email'=>'',
            'storeid'=>''
        );
        return $args;
    }


    public static function dataGetVipCard()
    {
        $args = array(
            // 条件类型 1为vipid，2为手机号，必需
            'condType'=>2,
            // 条件值，condType=1时传vipid, condType =2时传手机号，必需
            'condValue'=>'15398091110',
        );
        return $args;
    }


    public static function dataCheckVipCard()
    {

        $args = array(
            'idno'=>'142703198701143319',
            'phone'=>'15398091110',
        );
        return $args;
    }


    public static function dataGetVipScore()
    {

        $args = array(
            'vipid'=>'10017638',
        );
        return $args;
    }


    public static function dataExchangeGift()
    {

        $args = array(
            'vipid'=>'10017638',
            'password'=>'',
            'gift_score'=>'10',
        );
        return $args;
    }

    public static function dataAddVipScore()
    {

        $args = array(
            'vipid'=>'10017638',
            'storeid'=>'',
            'score'=>'10',
        );
        return $args;
    }

    public static function dataDecVipScore()
    {

        $args = array(
            'vipid'=>'10017638',
            'password'=>'',
            'changeType'=>'',
            'score'=>'10',
            'UniqueCode'=>'',
        );
        return $args;
    }

    public static function dataParkDecVipScore()
    {

        $args = array(
            'vipid'=>'10017638',
            'password'=>'',
            'changeType'=>'',
            'score'=>'10',
            'UniqueCode'=>'',
        );
        return $args;
    }


    public static function dataCheckDecVipScore()
    {

        $args = array(
            'UniqueCode'=>'',
            'score'=>'',
        );
        return $args;
    }

    public static function dataCancelDecVipScore()
    {

        $args = array(
            'UniqueCode'=>'',
            'score'=>'',
        );
        return $args;
    }


    public static function dataUpdateMemberInfo()
    {

        $args = array(
            'vipid'=>'10017638',
            'newaddress'=>'',
            'newphone'=>'',
        );
        return $args;
    }

    public static function dataGetVipCoupon()
    {

        $args = array(
            'vipid'=>'10017638',
        );
        return $args;
    }

    public static function dataExchangeScoreCoupon()
    {

        $args = array(
            'vipid'=>'10017638',
            'password'=>'',
            'ExchangeScore'=>'10',
        );
        return $args;
    }

    public static function dataCancelbindCard()
    {

        $args = array(
            'vipid'=>'10017638',
            'idno'=>'142703198701143319',
            'phone'=>'',
        );
        return $args;
    }


    public static function dataSetVipPassword()
    {

        $args = array(
            'vipid'=>'10017638',
            'password_old'=>'',
            'password'=>'123456',
        );
        return $args;
    }


    public static function dataGetVipSaleItem()
    {

        $args = array(
            'vipid'=>'10017638',
            'date_begin'=>'2017-08-15',
            'date_end'=>'2017-11-22',
        );
        return $args;
    }


    public static function dataGetVipScoreChangeItem()
    {

        $args = array(
            'vipid'=>'10017638',
            'date_begin'=>'2017-08-15',
            'date_end'=>'2017-11-22',
        );
        return $args;
    }

    public static function dataGetVipValidEndItem()
    {

        $args = array(
            'date_begin'=>'2017-08-15',
            'date_end'=>'2017-11-22',
        );
        return $args;
    }

    public static function dataGetVipUpgradeItem()
    {

        $args = array(
            'date_begin'=>'2017-08-15',
            'date_end'=>'2017-11-22',
        );
        return $args;
    }
}